﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using PR.BusinessEntity.RamanSyrtsou;

namespace PR.UI.Web.RamanSyrtsou
{
    public partial class Object : System.Web.UI.Page
    {
        public PRObjectRamanSyrtsou CurrentObject
        {
            get { return (PRObjectRamanSyrtsou)ViewState["CurrentObject"]; }
            set { ViewState["CurrentObject"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            NameValueCollection queryString = HttpContext.Current.Request.QueryString;

            if (_Default.ContainsKey(queryString, "descriptionId") && _Default.ContainsKey(queryString, "action") && queryString["action"] == "deleteDescription")
            {
                new WebServiceClientRamanSyrtsou().DeleteDescriptionById(int.Parse(queryString["descriptionId"]));
            }

            if (!Page.IsPostBack)
            {
                int prObjectID = 0;
                CurrentObject = new PRObjectRamanSyrtsou();

                if (_Default.ContainsKey(queryString, "id"))
                {
                    prObjectID = int.Parse(queryString["id"]);
                }

                CurrentPRObjectID.Value = prObjectID.ToString();

                if (Type.Items.Count == 0)
                {
                    foreach (var type in EnumerationsProvider.Instance.Types)
                    {
                        Type.Items.Add(new ListItem(type.Value, type.Key.ToString()));
                    }
                }
                if (Category.Items.Count == 0)
                {
                    foreach (var category in EnumerationsProvider.Instance.Categories)
                    {
                        Category.Items.Add(new ListItem(category.Value, category.Key.ToString()));
                    }
                }

                if (prObjectID > 0)
                {
                    PRObjectRamanSyrtsou obj = new WebServiceClientRamanSyrtsou().GetObjectById(prObjectID);
                    CurrentObject = obj;
                    Name.Text = obj.Name;
                    Type.SelectedIndex = Type.Items.IndexOf(Type.Items.Cast<ListItem>().First(i => i.Value == ((int)obj.Type).ToString()));
                    Category.SelectedIndex = Category.Items.IndexOf(Category.Items.Cast<ListItem>().First(i => i.Value == ((int)obj.Category).ToString()));

                    Descriptions.DataSource = CurrentObject.Descriptions;
                    Descriptions.DataBind();
                }
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {            
            if (int.Parse(CurrentPRObjectID.Value) > 0)
            {
                // update
                MapObjectFromUI();

                new WebServiceClientRamanSyrtsou().UpdatePRObject(CurrentObject);
            }
            else
            {
                // insert
                MapObjectFromUI();
                CurrentObject.CreationDate = DateTime.UtcNow;

                CurrentObject.ID = new WebServiceClientRamanSyrtsou().InsertPRObject(CurrentObject);

                CurrentPRObjectID.Value = CurrentObject.ID.ToString();
            }
        }

        private void MapObjectFromUI()
        {
            CurrentObject.Name = Name.Text;
            CurrentObject.Type = (PRObjectTypeRamanSyrtsou)int.Parse(Type.SelectedItem.Value);
            CurrentObject.Category = (PRObjectCategoryRamanSyrtsou)int.Parse(Category.SelectedItem.Value);
        }
    }
}
